package com.roadjava.javaweb.student.filter;

import com.roadjava.javaweb.student.beans.entity.AdminDO;
import com.roadjava.javaweb.student.util.Constants;
import com.roadjava.javaweb.student.util.ExcludeResourceUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter(filterName = "authorizationFilter"
        ,urlPatterns = {"/*"}
)
public class AuthorizationFilter  implements Filter {
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) req;
        // 过滤器排除静态资源 /student_javaweb/loginServlet
        String requestURI = httpServletRequest.getRequestURI();
        if (ExcludeResourceUtil.shouldExclude(requestURI)) {
            chain.doFilter(req, resp);
        }else {
            HttpSession session = httpServletRequest.getSession();
            AdminDO admin = (AdminDO) session.getAttribute("admin");
            String contextPath = httpServletRequest.getContextPath();
            String path = requestURI.substring(contextPath.length());
            if (admin == null) {
                if ("/loginServlet".equals(path)) {
                    chain.doFilter(req, resp);
                } else { // 未登录且访问的路径不是公共路径
                    httpServletRequest.getRequestDispatcher("/WEB-INF/login.jsp")
                            .forward(req,resp);
                }
            } else {
                chain.doFilter(req, resp);
            }
        }

    }

}
